/*
 * Copyright (C) 2023 HiHope Open Source Organization .
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "hi_io.h"
#include "iot_pwm.h"

// 硬件定义
#define IOT_GPIO_KEY        14
#define LED_TASK_STACK_SIZE 512
#define LED_TASK_PRIO       25

// PWM通道映射
typedef enum {
    RED_PWM = 2,
    GREEN_PWM = 1,
    BLUE_PWM = 0
} PwmChannel;

// LED引脚定义
typedef enum {
    RED_PIN = 2,
    GREEN_PIN = 7,
    BLUE_PIN = 8
} LedPin;

// PWM参数
#define DUTY_CYCLE_STEPS   100
#define PWM_FREQ_DIVISION  64000
#define BREATHE_DELAY_US   25000

// 全局状态
static enum { RED, GREEN, BLUE } g_ledState = RED;
static volatile int g_cycleCount = 100;  // 添加volatile保证可见性

// 呼吸灯效果函数
static void BreatheEffect(PwmChannel channel) {
    
    for (int i = DUTY_CYCLE_STEPS; i >= 1; i /= 2) {
        IoTPwmStart(channel, i, PWM_FREQ_DIVISION);
        usleep(BREATHE_DELAY_US);
    }
}

// LED控制线程
static void *LedController(void *arg) {
    (void)arg;
    
    while (g_cycleCount > 0) {
        switch (g_ledState) {
            case RED:
                BreatheEffect(RED_PWM);
                break;
            case GREEN:
                BreatheEffect(GREEN_PWM);
                break;
            case BLUE:
                BreatheEffect(BLUE_PWM);
                break;
        }
        
        // 关闭所有PWM通道
        IoTPwmStop(RED_PWM);
        IoTPwmStop(GREEN_PWM);
        IoTPwmStop(BLUE_PWM);
    }
    return NULL;
}

// 按键中断处理
static void OnButtonPressed(void) {
    g_ledState = (g_ledState + 1) % 3;  
    g_cycleCount--;                    
}

// 硬件初始化
static void HardwareInit(void) {
    
    IoTGpioInit(RED_PIN);
    IoTGpioInit(GREEN_PIN);
    IoTGpioInit(BLUE_PIN);
    IoTGpioInit(IOT_GPIO_KEY);

    
    hi_io_set_func(RED_PIN, HI_IO_FUNC_GPIO_2_PWM2_OUT);
    hi_io_set_func(GREEN_PIN, HI_IO_FUNC_GPIO_7_PWM0_OUT);
    hi_io_set_func(BLUE_PIN, HI_IO_FUNC_GPIO_8_PWM1_OUT);
    hi_io_set_func(IOT_GPIO_KEY, HI_IO_FUNC_GPIO_14_GPIO);

   
    IoTGpioSetDir(IOT_GPIO_KEY, IOT_GPIO_DIR_IN);
    hi_io_set_pull(IOT_GPIO_KEY, 1);  

 
    IoTPwmInit(RED_PWM);
    IoTPwmInit(GREEN_PWM);
    IoTPwmInit(BLUE_PWM);

   
    IoTGpioRegisterIsrFunc(IOT_GPIO_KEY, IOT_INT_TYPE_EDGE, 
                         IOT_GPIO_EDGE_FALL_LEVEL_LOW, 
                         OnButtonPressed, NULL);
}

// 主入口函数
static void LedExampleEntry(void) {
    HardwareInit();
    
    osThreadAttr_t attr = {
        .name = "LedController",
        .stack_size = LED_TASK_STACK_SIZE,
        .priority = LED_TASK_PRIO,
    };
    
    if (osThreadNew(LedController, NULL, &attr) == NULL) {
        printf("[ERROR] Failed to create LED control thread\n");
    }
}

SYS_RUN(LedExampleEntry);